package com.alquiler.dao;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.TemporalType;

import com.alquiler.dto.ItemDto;
import com.alquiler.entities.Reserva;
import com.alquiler.implementation.GenericDaoImpl;


public class ReservaDao extends GenericDaoImpl<Reserva, Integer, ItemDto> {

	public ReservaDao(EntityManager entityManager) {
		super(entityManager);
		
	}
	
	
	public List<Reserva> getTopDiezReservasByCancha(Integer idCancha){
		
		try {			
			Query query  =  entityManager.createQuery("Select r FROM " + Reserva.class.getCanonicalName()+" r "
					+ "inner join r.canchas c "
					+ "where c.idCancha = :idCancha "
					+ "order by r.fecha desc");
			 
			query.setParameter("idCancha",idCancha);			
			query.setMaxResults(10);
		
			List<Reserva> resultList = query.getResultList()  ;
		
			return resultList;
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}		
	}
	
	public List<Reserva> getReservasByCancha(Integer idCancha){
		
		try {			
			Query query  =  entityManager.createQuery("Select r FROM " + Reserva.class.getCanonicalName()+" r "
					+ "inner join r.canchas c "
					+ "where c.idCancha = :idCancha "
					+ "order by r.fecha desc");
			 
			query.setParameter("idCancha",idCancha);			
			
			List<Reserva> resultList = query.getResultList()  ;
		
			return resultList;
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}		
	}
	
	
	public List<Reserva> getReservasByUsuario(Integer idUsuario){
		
		try {			
			Query query  = entityManager.createQuery("FROM " + Reserva.class.getCanonicalName()+ " r "
					+ "WHERE r.usuario.idUsuario = :idUsuario ");
			
	    	query.setParameter("idUsuario", idUsuario);
	    	
			List<Reserva> resultList = query.getResultList()  ;		
			return resultList;
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}		
	}
	
	
	public List<Reserva> getReservasByfechaInicial(Date fechaIncial){
		
		try {			
			Query query  =  entityManager.createQuery("Select r FROM " + Reserva.class.getCanonicalName()+" r "
					+ "where r.fecha >= :fechaIncial "
					+ "order by r.fecha desc");
			 
			query.setParameter("fechaIncial",fechaIncial ,TemporalType.DATE);			
		
			List<Reserva> resultList = query.getResultList()  ;
		
			return resultList;
			
		} catch (Exception e) {
		
			return null;
		}		
	}
	
	
	

}
